package cn.kgc.controller;


import cn.kgc.constants.Constants;
import cn.kgc.dto.MaintainOrderDto;
import cn.kgc.dto.Result;
import cn.kgc.mapper.MaintainOrderMapper;
import cn.kgc.pojo.Evaluate;
import cn.kgc.pojo.MaintainOrder;
import cn.kgc.pojo.Users;
import cn.kgc.service.EvaluateService;
import cn.kgc.service.MaintainOrderService;
import cn.kgc.utils.redis.RedisKeyUtils;
import cn.kgc.utils.redis.RedisUtils;
import cn.kgc.utils.redis.ResultUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * <p>
 * 维修订单 前端控制器
 * </p>
 *
 * @author qf
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/api/order/maintain")
public class MaintainOrderController {


    private final RedisUtils redisUtils;
    private final MaintainOrderService maintainOrderService;
    private final EvaluateService    evaluateService;
    @Autowired
    private MaintainOrderMapper maintainOrderMapper;

    public MaintainOrderController( MaintainOrderMapper maintainOrderMapper, RedisUtils redisUtils, MaintainOrderService maintainOrderService, EvaluateService evaluateService) {

        this.redisUtils = redisUtils;
        this.maintainOrderService = maintainOrderService;
        this.evaluateService = evaluateService;
    }

    @PostMapping("/submit")
    public Result<MaintainOrder> submit(Long evaluateId, String phone, String adress, String appintDate, Integer temporalInterval, HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        System.out.println("token = " + token);
        String userJson = redisUtils.getValue(RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_TOKEN, token));
        Users users = JSON.parseObject(userJson, Users.class);
        System.out.println("users = " + users);
        Long id = users.getId();
        System.out.println("id = " + id);
//        map.put("evaluateId",evaluateId);
//        map.put("phone",phone);
//        map.put("adress",adress);
//        map.put("appintDate",appintDate);
//        map.put("temporalInterval",temporalInterval);
//        map.put("request",request.getHeader("token"));
//        System.out.println("evaluateId = " + evaluateId);
//        System.out.println("phone = " + phone);
//        System.out.println("adress = " + adress);
//        System.out.println("appintDate = " + appintDate);
//        System.out.println("temporalInterval = " + temporalInterval);
//        System.out.println("request = " + request.getHeader("token"));
//        String userJson = redisUtils.getValue(RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_TOKEN, token));
//
//        Users users = JSON.parseObject(userJson, Users.class);
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        Appointment appointment = new Appointment(users.getId(),phone,format.parse(appintDate),temporalInterval,adress,1,users.getId(),format.parse(format.format(new Date())),0);
//        appointmentService.save(appointment);
        MaintainOrder maintainOrder = new MaintainOrder();
        //order
        maintainOrder.setOrderNo("");
        Evaluate byId = evaluateService.getById(evaluateId);
        //userId
        maintainOrder.setModelId(byId.getCreatedUserId());
        //model
        maintainOrder.setUserId(id);
        maintainOrder.setEvaluateId(evaluateId);
        maintainOrder.setModelId(byId.getModelId());
        maintainOrder.setSubscription(byId.getSubscription());
        maintainOrder.setPrice(byId.getPrice());
        maintainOrder.setStatus(1);
        maintainOrder.setIsComment(0);
        maintainOrder.setCreatedUserId(byId.getCreatedUserId());
        maintainOrder.setCreatdTime(new Date());
        maintainOrder.setUpdatedTime(new Date());
        maintainOrderService.save(maintainOrder);
        Random random = new Random();
        int i = random.nextInt(100000000)+10000000;
        int b = random.nextInt(100000000)+10000000;
        MaintainOrder byId1 = maintainOrderService.getById(maintainOrder.getId());
        System.out.println("byId1 = " + byId1);
        Long id1 = byId1.getId();
        SimpleDateFormat format = new SimpleDateFormat("yyyyHHss");
        String format1 = format.format(new Date());
        byId1.setOrderNo(format1+i+b+maintainOrder.getId());
        String orderNo = byId1.getOrderNo();
        maintainOrderMapper.update(id1,orderNo);
        System.out.println(orderNo);
        MaintainOrderDto maintainOrderDto = maintainOrderMapper.getevaluateById(evaluateId);
        Result result = ResultUtils.returnDataSuccess(maintainOrderDto);
        return result;
    }

}

